<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Warehouses extends CI_Controller {

	public function index()
	{
		$this->load->model('Warehouses_model');
	    $id = null;
	    $name = null;
	    $text = null;
	    if ($this->input->post('id')) {
	        $id = $this->input->post('id');
	    }
	    if ($this->input->post('name')) {
	        $name = $this->input->post('name');
	    }
	    if ($this->input->post('text')) {
	        $text = $this->input->post('text');
	    }
	    $warehouses = $this->Warehouses_model->getAll();
	    $supplier = $this->Warehouses_model->getAll();
	    if ($id != null || $name != null || $text != null) {
	        $warehouses = $this->Warehouses_model->search($id, $name, $text);
	    }
	    $vars['warehouses'] = $warehouses;
	     $vars['supplier'] = $supplier;
	    $vars['id'] = $id;
	    $vars['name'] = $name;
	    $vars['text'] = $text;
	    $vars['content_view'] = 'warehouses_list_index';
	      $vars['content_view'] = 'warehouses_xproducts';
	      $vars['content_view'] = 'warehouses_list_index';
	    $vars['menu'] = 'warehouses';
	    $this->load->view('template', $vars);
	}

	public function add()
	{
		$this->load->model('Warehouses_model');
		// $vars['warehouses'] = $this->Product_groups_model->getAll();
	    $vars['content_view'] = 'warehouses_form';
	    $vars['menu'] = 'warehouses';
	    $this->load->view('template', $vars);
	}

	// public function evalue()
	// {
	//     if ($this->input->post('description')) {
	//         $desc = $this->input->post('description');
	//         $this->load->model('Product_groups_model');
	//         $dataGroup = $this->Product_groups_model->find($desc);
	//         if ($dataGroup) {
	//             echo "true";
	//         } else {
	//             echo "false";
	//         }
	//     }
	// }

	public function submit()
	{
	    $id = $this->input->post('warehouse_id');
	    if ($this->_validate_submit() === FALSE) {
	        if (isset($id) && is_numeric($id)) {
	            $this->edit($id);
	        } else {
	            $this->add();
	        }
	        return;
	    } else {
	        $this->load->model('Warehouses_model');
	        $data = array();
	        $data['name'] = $this->input->post('name');
	        $data['text'] = $this->input->post('text');
	        $data['status'] = 1;
	    }
	    if (isset($id) && is_numeric($id)) {
	        $this->Warehouses_model->update($id, $data);
	    } else {
	        $this->Warehouses_model->add($data);
	    }
	    redirect('/warehouses/', 'refresh');
	}

	public function edit($code)
	{
		$this->load->model('Warehouses_model');	    
	    $vars['warehouse'] = $this->Warehouses_model->find($code);
	    $vars['content_view'] = 'warehouses_form';
	    $vars['menu'] = 'warehouses';
	    $this->load->view('template', $vars);
	}

	public function delete($code)
	{
	    $this->load->model('Warehouses_model');
	   // $this->load->model('Warehouses_product_model');
        $this->Warehouses_model->delete($code);
      //  $this->Warehouses_product_model->delete($code);
        $this->index();
	}

	private function _validate_submit()
	{
	    $this->form_validation->set_rules('name', 'Nombre','trim|required');
	    $this->form_validation->set_rules('text', 'Dirección','trim|required');
	    return $this->form_validation->run();
	}


}